# !/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import time


def count_time(func):
    start_time = time.time()
    func()
    end_time = time.time()
    execution_time = (end_time - start_time)
    print("Execution time using time():", execution_time, "s")


def count_time2(func):
    start_time = time.perf_counter()
    func()
    end_time = time.perf_counter()
    execution_time = (end_time - start_time)
    print("Execution time using time():", execution_time, "s")


def count_time3(func):
    start_time = datetime.datetime.now()
    func()
    end_time = datetime.datetime.now()
    execution_time = (end_time - start_time).total_seconds()
    print("Execution time using time():", execution_time, "s")


if __name__ == '__main__':
    # 时间戳
    t = time.time()
    t1 = time.strftime('%Y-%m-%d %H:%M:%S')
    t2 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    t3 = time.strptime('2024-03-08 17:07:14', '%Y-%m-%d %H:%M:%S')
    print(t, t1, t2, t3, type(t), type(t1), type(t2), type(t3))
    print(t3, type(t3))

    # 时间， 年月日，时分秒
    t = datetime.datetime.now()
    print(t)
    # 时间，字符串相互转换
    str_date = t.strftime("%Y-%m-%d %H:%M:%S")
    print(str_date, type(str_date))
    d = t.strptime(str_date, "%Y-%m-%d %H:%M:%S")
    print(d, type(d))

    # 计时
    count_time(lambda: time.sleep(0.1))
    count_time2(lambda: time.sleep(0.1))
    count_time3(lambda: time.sleep(0.1))